//
//  CNUILabel.swift
//  YiZhangGui
//
//  Created by 卢逢卫 on 2024/4/8.
//

import UIKit

extension UILabel {
    /// 使用指定的文本、文本颜色和字体初始化标签
    ///
    /// - Parameters:
    ///   - text: 标签的文本
    ///   - textColor: 标签的文本颜色
    ///   - font: 标签的字体
    public convenience init(text: String, textColor: UIColor, font: UIFont) {
        self.init()
        self.text = text
        self.textColor = textColor
        self.font = font
    }
    
    convenience init(text: String, textColor: UIColor, font: UIFont, numberOfLines: Int) {
            self.init()
            self.text = text
            self.textColor = textColor
            self.font = font
            self.numberOfLines = numberOfLines
        }
    
    convenience init(background: UIColor, text: String, textColor: UIColor, font: UIFont) {
            self.init()
            self.backgroundColor = background
            self.text = text
            self.textColor = textColor
            self.font = font
        }
    
    /// 使用指定的背景颜色、文本、文本颜色、字体和对齐方式初始化标签
        ///
        /// - Parameters:
        ///   - background: 标签的背景颜色
        ///   - text: 标签的文本
        ///   - textColor: 标签的文本颜色
        ///   - font: 标签的字体
        ///   - alignment: 标签的对齐方式
        /// - Returns: 初始化后的标签
        public convenience init(background: UIColor, text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment) {
            self.init()
            self.backgroundColor = background
            self.text = text
            self.textColor = textColor
            self.font = font
            self.textAlignment = alignment
        }
    
    convenience init(text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment) {
            self.init()
            self.text = text
            self.textColor = textColor
            self.font = font
            self.textAlignment = alignment
        }
    
    convenience init(text: String, textColor: UIColor, font: UIFont, numberOfLines: Int, lineSpacing: CGFloat) {
            self.init()
            self.text = text
            self.textColor = textColor
            self.font = font
            self.numberOfLines = numberOfLines
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = lineSpacing
            let attributedString = NSAttributedString(string: text, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
            self.attributedText = attributedString
        }
    
    convenience init(background: UIColor, text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment, numberOfLines: Int, lineSpacing: CGFloat) {
            self.init()
            self.backgroundColor = background
            self.text = text
            self.textColor = textColor
            self.font = font
            self.textAlignment = alignment
            self.numberOfLines = numberOfLines
            
            if numberOfLines != 1 {
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.lineSpacing = lineSpacing
                let attributedString = NSAttributedString(string: text, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
                self.attributedText = attributedString
            }
        }
    
    convenience init(text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment, numberOfLines: Int) {
            self.init()
            self.text = text
            self.textColor = textColor
            self.font = font
            self.textAlignment = alignment
            self.numberOfLines = numberOfLines
        }
}




extension UILabel {
    
    /// 使用指定的文本、文本颜色和字体初始化标签
    ///
    /// - Parameters:
    ///   - text: 标签的文本
    ///   - textColor: 标签的文本颜色
    ///   - font: 标签的字体
    /// - Returns: 初始化后的标签
    public static func cn_initWith(text: String, textColor: UIColor, font: UIFont) -> Self {
        let label = self.init()
        label.text = text
        label.textColor = textColor
        label.font = font
        return label
    }
    
    /// 使用指定的文本、文本颜色、字体和对齐方式初始化标签
    ///
    /// - Parameters:
    ///   - text: 标签的文本
    ///   - textColor: 标签的文本颜色
    ///   - font: 标签的字体
    ///   - alignment: 标签的对齐方式
    /// - Returns: 初始化后的标签
    public static func cn_initWith(text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment) -> Self {
        let label = self.init()
        label.text = text
        label.textColor = textColor
        label.font = font
        label.textAlignment = alignment
        return label
    }
    
    /// 使用指定的背景颜色、文本、文本颜色、字体和对齐方式初始化标签
        ///
        /// - Parameters:
        ///   - background: 标签的背景颜色
        ///   - text: 标签的文本
        ///   - textColor: 标签的文本颜色
        ///   - font: 标签的字体
        ///   - alignment: 标签的对齐方式
        /// - Returns: 初始化后的标签
        public static func cn_initWith(background: UIColor, text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment) -> Self {
            let label = self.init()
            label.backgroundColor = background
            label.text = text
            label.textColor = textColor
            label.font = font
            label.textAlignment = alignment
            return label
        }
}

extension UILabel {
    /// 使用指定的背景颜色、文本、文本颜色、字体、对齐方式、行数和行间距初始化标签
    ///
    /// - Parameters:
    ///   - background: 标签的背景颜色
    ///   - text: 标签的文本
    ///   - textColor: 标签的文本颜色
    ///   - font: 标签的字体
    ///   - alignment: 标签的对齐方式
    ///   - numberOfLines: 标签的行数
    ///   - lineSpacing: 标签的行间距
    /// - Returns: 初始化后的标签
    public static func cn_initWith(background: UIColor, text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment, numberOfLines: Int, lineSpacing: CGFloat) -> Self {
        let label = self.init()
        label.backgroundColor = background
        label.text = text
        label.textColor = textColor
        label.font = font
        label.textAlignment = alignment
        label.numberOfLines = numberOfLines
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
        label.attributedText = attributedString
        
        return label
    }
    
    convenience init(text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment, numberOfLines: Int, lineSpacing: CGFloat) {
           self.init()
           self.text = text
           self.textColor = textColor
           self.font = font
           self.textAlignment = alignment
           self.numberOfLines = numberOfLines
           
           let paragraphStyle = NSMutableParagraphStyle()
           paragraphStyle.lineSpacing = lineSpacing
           let attributedString = NSMutableAttributedString(string: text)
           attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
           self.attributedText = attributedString
       }
}

extension UILabel {
    /// 使用指定的背景颜色、文本、文本颜色、字体、对齐方式、行数和行间距初始化标签
    ///
    /// - Parameters:
    ///   - background: 标签的背景颜色
    ///   - text: 标签的文本
    ///   - textColor: 标签的文本颜色
    ///   - font: 标签的字体
    ///   - alignment: 标签的对齐方式
    ///   - numberOfLines: 标签的行数
    ///   - lineSpacing: 标签的行间距
    /// - Returns: 初始化后的标签
    public static func cn_initWith(background: UIColor, text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment, numberOfLines: Int, lineSpacing: CGFloat?) -> Self {
        let label = self.init()
        label.backgroundColor = background
        label.text = text
        label.textColor = textColor
        label.font = font
        label.textAlignment = alignment
        label.numberOfLines = numberOfLines
        
        if let lineSpacing = lineSpacing {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = lineSpacing
            let attributedString = NSMutableAttributedString(string: text)
            attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
            label.attributedText = attributedString
        }
        
        return label
    }
    
    /// 使用指定的背景颜色、文本、文本颜色、字体、对齐方式和行数初始化标签
        ///
        /// - Parameters:
        ///   - background: 标签的背景颜色
        ///   - text: 标签的文本
        ///   - textColor: 标签的文本颜色
        ///   - font: 标签的字体
        ///   - alignment: 标签的对齐方式
        ///   - numberOfLines: 标签的行数
        /// - Returns: 初始化后的标签
        public static func cn_initWith(background: UIColor, text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment, numberOfLines: Int) -> Self {
            let label = self.init()
            label.backgroundColor = background
            label.text = text
            label.textColor = textColor
            label.font = font
            label.textAlignment = alignment
            label.numberOfLines = numberOfLines
            
            return label
        }
    
    
    public static func cn_initWith(text: String, textColor: UIColor, font: UIFont, numberOfLines: Int, lineSpacing: CGFloat) -> Self {
            let label = self.init()
            label.text = text
            label.textColor = textColor
            label.font = font
            label.numberOfLines = numberOfLines
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = lineSpacing
            let attributedString = NSMutableAttributedString(string: text)
            attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
            label.attributedText = attributedString
            
            return label
        }
    
    public static func cn_initWith(text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment, numberOfLines: Int) -> Self {
           let label = self.init()
           label.text = text
           label.textColor = textColor
           label.font = font
           label.textAlignment = alignment
           label.numberOfLines = numberOfLines
           return label
       }
    
    public static func cn_initWith(text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment, numberOfLines: Int, lineSpacing: CGFloat) -> Self {
            let label = self.init()
            label.text = text
            label.textColor = textColor
            label.font = font
            label.textAlignment = alignment
            label.numberOfLines = numberOfLines
            
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = lineSpacing
            let attributedString = NSMutableAttributedString(string: text)
            attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: attributedString.length))
            label.attributedText = attributedString
            
            return label
        }
    
    public static func cn_initWith(text: String, textColor: UIColor, font: UIFont, numberOfLines: Int) -> Self {
            let label = self.init()
            label.text = text
            label.textColor = textColor
            label.font = font
            label.numberOfLines = numberOfLines
            return label
        }
    
    public static func cn_initWith(background: UIColor, text: String, textColor: UIColor, font: UIFont) -> Self {
            let label = self.init()
            label.backgroundColor = background
            label.text = text
            label.textColor = textColor
            label.font = font
            return label
        }
}


